//
//  TabBarControllerConfig.m
//  XV
//
//  Created by apple on 2019/03/11.
//  Copyright © 2019 XV. All rights reserved.
//

#import "TabBarControllerConfig.h"
#import "BaseNaviViewController.h"
#import "XSJLiveVC.h"
#import "XSJEntertainmentVC.h"
#import "XSJDynamicVC.h"
#import "XSJMeVC.h"
#import "CYLPlusButtonSubclass.h"


@interface TabBarControllerConfig ()<UITabBarControllerDelegate>

@property (nonatomic, readwrite, strong) BaseTabBarController *tabBarController;

@end


@implementation TabBarControllerConfig


/**
 *  lazy load tabBarController
 *
 *  @return CYLTabBarController
 */
- (CYLTabBarController *)tabBarController {
    if (_tabBarController == nil) {
        /**
         * 以下两行代码目的在于手动设置让TabBarItem只显示图标，不显示文字，并让图标垂直居中。
         * 等效于在 `-tabBarItemsAttributesForController` 方法中不传 `CYLTabBarItemTitle` 字段。
         * 更推荐后一种做法。
         */
        UIEdgeInsets imageInsets = UIEdgeInsetsZero;//UIEdgeInsetsMake(4.5, 0, -4.5, 0);
        UIOffset titlePositionAdjustment = UIOffsetZero;//UIOffsetMake(0, MAXFLOAT);
        
        _tabBarController = [BaseTabBarController tabBarControllerWithViewControllers:self.viewControllers
                                                                tabBarItemsAttributes:self.tabBarItemsAttributesForController
                                                                          imageInsets:imageInsets
                                                              titlePositionAdjustment:titlePositionAdjustment];
        
        // 拿到 TabBar 在拿到想应的item
        UITabBar *tabBar = _tabBarController.tabBar;
        //修改选中字体颜色
        tabBar.tintColor = [UIColor blackColor];
        //未选中字体颜色
        tabBar.unselectedItemTintColor = [UIColor grayColor];
        //tabBar.backgroundColor = MainColor;
        
        //修改背景颜色
        // 设置一个自定义 View,大小等于 tabBar 的大小
        UIView *bgView = [[UIView alloc] initWithFrame:tabBar.bounds];
        // 给自定义 View 设置颜色
        bgView.backgroundColor = [UIColor whiteColor];
        // 将自定义 View 添加到 tabBar 上
        [tabBar insertSubview:bgView atIndex:0];
    }
    return _tabBarController;
}


- (NSArray *)viewControllers
{
    XSJLiveVC *firstViewController = [[XSJLiveVC alloc] init];
    UIViewController *firstNavigationController = [[BaseNaviViewController alloc]
                                                   initWithRootViewController:firstViewController];
    
    XSJEntertainmentVC *secondViewController = [[XSJEntertainmentVC alloc] init];
    UIViewController *secondNavigationController = [[BaseNaviViewController alloc]
                                                    initWithRootViewController:secondViewController];
    
    XSJDynamicVC *thirdViewController = [[XSJDynamicVC alloc] init];
    UIViewController *thirdNavigationController = [[BaseNaviViewController alloc]
                                                   initWithRootViewController:thirdViewController];
    
    XSJMeVC *fourthViewController = [[XSJMeVC alloc] init];
    UIViewController *fourthNavigationController = [[BaseNaviViewController alloc]
                                                    initWithRootViewController:fourthViewController];
    
    
    NSArray *viewControllers = @[
                                 firstNavigationController,
                                 secondNavigationController,
                                 thirdNavigationController,
                                 fourthNavigationController
                                 ];
    return viewControllers;
}

- (NSArray *)tabBarItemsAttributesForController
{
    NSDictionary *firstTabBarItemsAttributes =
      @{
        CYLTabBarItemTitle : @"直播",
        CYLTabBarItemImage : @"live01",
        CYLTabBarItemSelectedImage : @"live02",
        };
      
      NSDictionary *secondTabBarItemsAttributes =
      @{
        CYLTabBarItemTitle : @"娱乐",
        CYLTabBarItemImage : @"pastime01",
        CYLTabBarItemSelectedImage : @"pastime02",
        };
      
      NSDictionary *thirdTabBarItemsAttributes =
    @{
        CYLTabBarItemTitle : @"动态",
        CYLTabBarItemImage : @"dynamic01",
        CYLTabBarItemSelectedImage : @"dynamic02",
      };
      
      NSDictionary *fourthTabBarItemsAttributes =
      @{
        CYLTabBarItemTitle : @"我的",
        CYLTabBarItemImage : @"me1",
        CYLTabBarItemSelectedImage : @"me2"
        };
      
      NSArray *tabBarItemsAttributes = @[
                                         firstTabBarItemsAttributes,
                                         secondTabBarItemsAttributes,
                                         thirdTabBarItemsAttributes,
                                         fourthTabBarItemsAttributes
                                         ];
      return tabBarItemsAttributes;
}


//注册+号按钮
-(void)regPlusButton
{
    [CYLPlusButtonSubclass registerPlusButton];
}

@end
